

using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store.Web.Controls;
using JTC.MiniCommerce.Store.Services;
using JTC.MiniCommerce.Store.Services.PaymentService;

namespace JTC.MiniCommerce.Web.admin.controls.configuration.paymentproviders {
  public partial class paypalstandardconfiguration : PaymentConfigurationControl {
  
    #region Member Variables

    ProviderSettings payPalStandardConfigurationSettings;
    PaymentServiceSettings paymentServiceSettings;
    
    #endregion
    
    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        SetPayPalStandardConfigurationProperties();
        paymentServiceSettings = PaymentService.FetchConfiguredPaymentProviders();
        if(paymentServiceSettings != null) {
          foreach(ProviderSettings providerSettings in paymentServiceSettings.ProviderSettingsCollection) {
            if(providerSettings.Name == typeof(PayPalStandardPaymentProvider).Name) {
              payPalStandardConfigurationSettings = providerSettings;
            }
          }
          if(payPalStandardConfigurationSettings != null) {
            bool isLive = false;
            bool isParsed = bool.TryParse(payPalStandardConfigurationSettings.Parameters[PayPalStandardPaymentProvider.ISLIVE], out isLive);
            chkIsLive.Checked = isLive;
            txtBusinessEmail.Text = payPalStandardConfigurationSettings.Parameters[PayPalStandardPaymentProvider.BUSINESS_EMAIL];
            txtPdtId.Text = payPalStandardConfigurationSettings.Parameters[PayPalStandardPaymentProvider.PDTID];
          }
        }
        else {
          paymentServiceSettings = new PaymentServiceSettings();
        }
      }
      catch(Exception ex) {
        Logger.Error(typeof(paypalstandardconfiguration).Name + ".Page_Load", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }     
    }

    /// <summary>
    /// Handles the Click event of the btnSave control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSave_Click(object sender, EventArgs e) {
      try {
        if(payPalStandardConfigurationSettings == null) {
          payPalStandardConfigurationSettings = new ProviderSettings(typeof(PayPalStandardPaymentProvider).Name, typeof(PayPalStandardPaymentProvider).AssemblyQualifiedName);
          paymentServiceSettings.ProviderSettingsCollection.Add(payPalStandardConfigurationSettings);
        }
        payPalStandardConfigurationSettings.Parameters.Clear();
        payPalStandardConfigurationSettings.Parameters.Add(PayPalStandardPaymentProvider.ISLIVE, chkIsLive.Checked.ToString());
        payPalStandardConfigurationSettings.Parameters.Add(PayPalStandardPaymentProvider.BUSINESS_EMAIL, txtBusinessEmail.Text.Trim());
        payPalStandardConfigurationSettings.Parameters.Add(PayPalStandardPaymentProvider.PDTID, txtPdtId.Text.Trim());
        int id = base.Save(paymentServiceSettings, WebUtility.GetUserName());
        if(id > 0) {
          MasterPage.MessageCenter.DisplaySuccessMessage(LocalizationUtility.GetText("lblPaymentConfigurationSaved"));
        }
        else {
          MasterPage.MessageCenter.DisplayFailureMessage(LocalizationUtility.GetText("lblPaymentConfigurationNotSaved"));
        }
      }
      catch(Exception ex) {
        Logger.Error(typeof(paypalstandardconfiguration).Name + ".btnSave_Click", ex);
        base.MasterPage.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }     
    }
    
    #endregion
    
    #region Methods
    
    #region Private

    /// <summary>
    /// Sets the pay pal standard configuration properties.
    /// </summary>
    private void SetPayPalStandardConfigurationProperties() {
      ltDescription.Text = HttpUtility.HtmlDecode(base.Provider.Description);
      pnlSettings.GroupingText = LocalizationUtility.GetText("pnlPayPalStandardConfiguration");
      lblDescriptionTitle.Text = LocalizationUtility.GetText("lblPayPalStandardDescriptionTitle");
      LocalizationUtility.AddButtonText(btnSave);
      LocalizationUtility.AddLabelText(lblIsLive);
      LocalizationUtility.AddHoverHelp(hlIsLiveHelp);
      LocalizationUtility.AddLabelText(lblBusinessEmail);
      LocalizationUtility.AddHoverHelp(hlBusinessEmailHelp);
      LocalizationUtility.AddLabelText(lblPdtId);
      LocalizationUtility.AddHoverHelp(hlPdtId);
      LocalizationUtility.AddRfvErrorMessageText(rfvBusinessEmail);
      LocalizationUtility.AddRfvErrorMessageText(rfvPdtId);
    }

    #endregion
    
    #endregion    
    
  }
}